<script setup>
import { ref } from "vue";
import { ElMessage } from "element-plus"; // 引入 Element Plus 的消息提示组件
import { useRouter } from "vue-router";
const router = useRouter();
// 定义数据模型
const passwordData = ref({
    old_pwd: "",
    new_pwd: "",
    re_pwd: "",
});

// 表单引用
const formRef = ref(null); // 用于引用 el-form 实例

// 校验确认密码
const checkRePassword = (rule, value, callback) => {
  if (!value) {
    callback(new Error("请再次确认密码"));
  } else if (value !== passwordData.value.new_pwd) {
    callback(new Error("两次输入的密码不一致"));
  } else {
    callback();
  }
};

// 定义表单验证规则
const rules = {
    old_pwd: [
    { required: true, message: "请输入原密码", trigger: "blur" },
    { min: 5, max: 16, message: "密码长度为5～16位", trigger: "blur" },
  ],
  new_pwd: [
    { required: true, message: "请输入新密码", trigger: "blur" },
    { min: 5, max: 16, message: "密码长度为5～16位", trigger: "blur" },
  ],
  repassword: [{ validator: checkRePassword, trigger: "blur" }],
};

import { userPasswordResetService } from "@/api/user.js";
import Login from "../Login.vue";
// 重置密码方法
const resetPassword = async () => {
  //调用接口
  let result = await userPasswordResetService(passwordData.value);
  console.log(result)
  if (result.code === 0) {
    ElMessage.success(result.message ? result.message : "重置成功");
    //重置表单
    router.push("/login");
  } else {
    ElMessage.error(result.message ? result.message : "重置失败");
  }
};
</script>


<template>
  <el-card >
    <template #header>
      <div class="header">
        <span>重置密码</span>
      </div>
    </template>
    <el-form
      label-position="top"
      :model="passwordData"
      :rules="rules"
      ref="formRef"
    >
      <el-form-item label="原密码：" prop="old_pwd">
        <el-input
          v-model="passwordData.old_pwd"
          type="password"
          style="width: 240px"
          placeholder="请输入原密码"
        />
      </el-form-item>
      <el-form-item label="新密码：" prop="new_pwd">
        <el-input
          v-model="passwordData.new_pwd"
          type="password"
          style="width: 240px"
          placeholder="请输入新密码"
        />
      </el-form-item>
      <el-form-item label="确认新密码：" prop="re_pwd">
        <el-input
          v-model="passwordData.re_pwd"
          type="password"
          style="width: 240px"
          placeholder="请再次输入新密码"
        />
      </el-form-item>
      <el-button type="primary" @click="resetPassword">重置密码</el-button>
    </el-form>
  </el-card>
</template>
  